package com.yuanli.modules.system.service.impl;


import com.yuanli.common.api.CommonAPI;
import com.yuanli.common.system.vo.DictModel;
import com.yuanli.common.system.vo.LoginUser;
import com.yuanli.common.utils.SensitiveInfoUtil;
import com.yuanli.common.utils.oConvertUtils;
import com.yuanli.modules.system.mapper.SysUserRoleMapper;
import com.yuanli.modules.system.service.ISysDictService;
import com.yuanli.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: 底层共通业务API，提供其他独立模块调用
 */
@Service
public class SysBaseApiImpl implements CommonAPI {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDictService sysDictService;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public LoginUser getUserByName(String username) {
        if (oConvertUtils.isEmpty(username)) {
            return null;
        }
        LoginUser user = sysUserService.getEncodeUserInfo(username);

        try {
            SensitiveInfoUtil.handlerObject(user, false);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public Set<String> queryUserRoles(String username) {
        // 查询用户拥有的角色集合
        List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
//        log.info("-------通过数据库读取用户拥有的角色Rules------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
        return new HashSet<>(roles);
    }

    @Override
    public Set<String> queryUserAuths(String username) {
        return this.getUserRoleSet(username);
    }

    /**
     * 查询用户拥有的角色集合
     *
     * @param username
     * @return
     */
    public Set<String> getUserRoleSet(String username) {
        // 查询用户拥有的角色集合
        List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
//        log.info("-------通过数据库读取用户拥有的角色Rules------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
        return new HashSet<>(roles);
    }
    @Override
    public List<DictModel> translateDictFromTableByKeys(String table, String text, String code, String keys) {
        return sysDictService.queryTableDictTextByKeys(table, text, code, Arrays.asList(keys.split(",")));
    }

    @Override
    public Map<String, List<DictModel>> translateManyDict(String dictCodes, String keys) {
        List<String> dictCodeList = Arrays.asList(dictCodes.split(","));
        List<String> values = Arrays.asList(keys.split(","));
        return sysDictService.queryManyDictByKeys(dictCodeList, values);
    }

    @Override
    public String translateDictFromTable(String table, String text, String code, String key) {
        return sysDictService.queryTableDictTextByKey(table, text, code, key);
    }

    @Override
    public String translateDict(String code, String key) {
        return sysDictService.queryDictTextByKey(code, key);
    }

}
